﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SunrayEngine.Maps;
using System;
using System.Linq;

namespace SunrayEngine.MapEditor.EditTools
{
    public sealed class EditableMapTile : MapTile
    {
        #region initialize
        public EditableMapTile(int x, int y, int width, int height, GraphicsDevice device)
            : base(x, y, width, height, device)
        {
            offset = Point.Zero;
            texture = new Texture2D(device, width, height);
            walkabelTexture = new Texture2D(device, width, height);
            bounds = new Rectangle(x, y, width, height);
            transformedBounds = bounds;
            pixels = new PixelInfo[width, height];

            for (int i = 0; i < width; i++)
                for (int j = 0; j < height; j++)
                    pixels[i, j].Color = Color.White;

            PixelsToTexture();
            WalkabelToTexture();

        }
        #endregion

        #region methods
        public void SetWalkabels(Brush brush, bool erase)
        {
            Point[] selPixels = selectedPixels(brush);

            foreach (var p in selPixels)
            {
                if (pixels[p.X, p.Y].Color != new Color(0, 0, 0, 0))
                {
                    if (erase)
                        pixels[p.X, p.Y].WalkAbel = false;
                    else
                        pixels[p.X, p.Y].WalkAbel = true;
                }
            }

            WalkabelToTexture();
        }

        public void PaintToTile(Brush brush, bool erase)
        {
            Color[] bTexCols = new Color[brush.PaintTexture.Width * brush.PaintTexture.Height];
            brush.PaintTexture.GetData<Color>(bTexCols);

            Point[] selPixels = selectedPixels(brush);

            foreach(var p in selPixels)
            {
                if (erase)
                {
                    if(pixels[p.X, p.Y].Color != new Color(0,0,0,0))
                        pixels[p.X, p.Y].Color = Color.Lerp(pixels[p.X, p.Y].Color, Color.White, ((float)brush.Alpha / 100f));
                    continue;
                }

                if (bTexCols.Length != 1)
                {
                    Color c = bTexCols[p.X + brush.PaintTexture.Width * p.Y];

                    pixels[p.X, p.Y].Color = Color.Lerp(pixels[p.X, p.Y].Color, c, ((float)brush.Alpha / 100f));
                }
                else
                {
                    Color c = bTexCols[0];
                    pixels[p.X, p.Y].Color = Color.Lerp(pixels[p.X, p.Y].Color, c, ((float)brush.Alpha / 100f));
                }
            }

            PixelsToTexture();
        }

        private Point[] selectedPixels(Brush brush)
        {
            List<Point> points = new List<Point>();

            Rectangle brushB = brush.GetBounds();
            Rectangle intersection = Rectangle.Intersect(transformedBounds, brushB);
            Rectangle result = new Rectangle(intersection.X - transformedBounds.X, intersection.Y - transformedBounds.Y,
                intersection.Width, intersection.Height);

            for(int x = result.X; x < transformedBounds.Width; x++)
                for (int y = result.Y; y < transformedBounds.Height; y++)
                    if (x >= 0 && x < result.Width + result.X &&
                        y >= 0 && y < result.Height + result.Y)
                    {
                        points.Add(new Point(x, y));
                    }

            return points.ToArray();
        }
        #endregion
    }
}
